package com.xyht.sca_s.student_manage_system.modules.qr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ExceptionCast;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.QrUtil;
import com.xyht.sca_s.student_manage_system.modules.qr.entity.SmsQr;
import com.xyht.sca_s.student_manage_system.modules.qr.entity.req.SmsQrReq;
import com.xyht.sca_s.student_manage_system.modules.qr.entity.resp.SmsQrResp;
import com.xyht.sca_s.student_manage_system.modules.qr.mapper.SmsQrMapper;
import com.xyht.sca_s.student_manage_system.modules.qr.service.SmsQrService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 二维码管理 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-03-22
 */
@Service
public class SmsQrServiceImpl extends ServiceImpl<SmsQrMapper, SmsQr> implements SmsQrService {
    @Autowired
    private SmsQrMapper smsQrMapper;

    @Override
    @Transactional
    public ResponseResult addQr(SmsQrReq smsQrReq) {
        if (isNullOrEmpty(smsQrReq.getQrType()) || isNullOrEmpty(smsQrReq.getQrPosition())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsQr smsQr = new SmsQr();
        smsQr.setQrType(smsQrReq.getQrType());
        smsQr.setQrPosition(smsQrReq.getQrPosition());
        smsQrMapper.insert(smsQr);

        String qr_id = smsQr.getId();
        if (isNullOrEmpty(qr_id)){
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.FAIL));
        }

        try {
            String qr_pic = QrUtil.crateQRCode(qr_id,200,200);
            SmsQr updateQr = new SmsQr();
            updateQr.setId(qr_id);
            updateQr.setQrPic(qr_pic);
            smsQrMapper.updateById(updateQr);
        }catch (Exception e){
            e.printStackTrace();
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.FAIL));
        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateQr(SmsQrReq smsQrReq) {
        if (isNullOrEmpty(smsQrReq.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsQr resultQr = smsQrMapper.selectById(smsQrReq.getId());
        if (resultQr == null || resultQr.getId() == null){
            return CommonResult.failed(CommonCodeEnum.QR_NOT_EXIST);
        }

        if (!isNullOrEmpty(smsQrReq.getQrPosition())){
            SmsQr smsQr = new SmsQr();
            smsQr.setId(smsQrReq.getId());
            smsQr.setQrPosition(smsQrReq.getQrPosition());
            smsQrMapper.updateById(smsQr);
        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult deleteQr(SmsQrReq smsQrReq) {
        if (isNullOrEmpty(smsQrReq.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsQr resultQr = smsQrMapper.selectById(smsQrReq.getId());
        if (resultQr == null || resultQr.getId() == null){
            return CommonResult.failed(CommonCodeEnum.QR_NOT_EXIST);
        }

        smsQrMapper.deleteById(smsQrReq.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult getQr(String qr_id, Integer qr_type, String search_str,Integer pageNum,Integer pageSize) {
        QueryWrapper<SmsQr> qrQueryWrapper = new QueryWrapper<>();
        qrQueryWrapper.lambda()
                .orderByDesc(SmsQr::getCreateTime)
                .orderByAsc(SmsQr::getQrType);
        if (!isNullOrEmpty(qr_id)){
            qrQueryWrapper.lambda().eq(SmsQr::getId,qr_id);
        }
        if (!isNullOrEmpty(qr_type)){
            qrQueryWrapper.lambda().eq(SmsQr::getQrType,qr_type);
        }
        if (!isNullOrEmpty(search_str)){
            qrQueryWrapper.lambda().like(SmsQr::getQrPosition,search_str);
        }
        Page<SmsQr> page = new Page<>(pageNum,pageSize);
        smsQrMapper.selectPage(page,qrQueryWrapper);
        List<SmsQrResp> respList;
        respList = page.getRecords().stream().map(qr->{
            SmsQrResp smsQrResp = new SmsQrResp();
            BeanUtils.copyProperties(qr,smsQrResp);
            return smsQrResp;
        }).collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(respList,total);
    }
}
